package a_aa_amainpackage.a_o2odata_deal.utils

import a_aa_amainpackage.a_o2odata_deal.config.config._
import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

import org.apache.spark.sql.{DataFrame, SQLContext, SparkSession}
import org.elasticsearch.spark._

object tm_toes {
  val indexs = s"${years}_${platform}/${platform}_${years}_${month}"
  //val indexs =  s"2019_presale_tmall/tmall_2019_shuang11_4"
  def restart(sc: SparkContext, sqlContext: SQLContext): Unit = {
    //原始代码
    //
    // val result = a_o2odata_deal.utils.handle_zhibo.handle_zhibo(sqlContext)
    val result = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${years}/${months}/zhibo_finally")
      //.drop("priceText").withColumnRenamed("priceText_v1","priceText")
      .where("cast(sellCount as bigint)>0 and cast(salesAmount as double)>0 and cast(priceText as double)>0")
      .dropDuplicates("good_id")

    //双十一代码
   /* val brand_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/li_yinchao/lyc/platform/tmall/2019/11/tmall_2019_11/brand_join/")
        //.drop("is_showLive")
      .where("cast(sellCount as bigint)>0 and cast(salesAmount as double)>0 and cast(priceText as double)>0")
      .dropDuplicates("good_id")*/
    /*brand_data.createOrReplaceTempView("brand_data")
    //添加直播
    val data1 = sqlContext.read.json("s3a://o2o-sourcedata/obs-source-2019/shuang11/presale/tmall/tmall_live_detail/")
    val data2 = sqlContext.read.json("s3a://o2o-sourcedata/obs-source-2019/shuang11/presale/tmall/tmall_live_id/")
    data1.createOrReplaceTempView("data1")
    data2.createOrReplaceTempView("data2")

    val data3 = sqlContext.sql(
      s"""
         |select
         |t1.*,
         |t2.good_id as good_ids
         |from data2 t1
         |inner join
         |data1 t2
         |on t1.liveId=t2.liveId
       """.stripMargin).drop("good_id").withColumnRenamed("good_ids","good_id").where("good_id!='-1'")

    data3.createOrReplaceTempView("data3")
    val data4 = sqlContext.sql(
      s"""
         |select
         |t1.*,
         |t2.*,
         |case
         |when t2.good_id is null then 'false'
         |else 'true'
         |end as is_showLive
         |from brand_data t1
         |left join
         |data3 t2
         |on t1.good_id=t2.good_id
       """.stripMargin)*/

    toes(result)
    xichenqicate(sc)
    computer_cate(sc,sqlContext)
    kongtiao_cate(sc,sqlContext)
     suannaicate(sc,sqlContext.sparkSession)
    //增加三元低温乳品旗舰店酸奶的处理
    handle_syqjd(sc,sqlContext)
      ruyemianshuang(sc,sqlContext.sparkSession)
     other_cate(sc,sqlContext)
     xuetangyi(sc,sqlContext)
  }


  def handle_syqjd(sc:SparkContext,sqlContext:SQLContext):Unit={
    val value1: RDD[String] = sc.esJsonRDD(s"${indexs}",
      """
        |{
        |  "query": {
        |    "bool": {
        |      "must": [
        |        {
        |          "term": {
        |            "shopName.keyword": {
        |              "value": "三元低温乳品旗舰店"
        |            }
        |          }
        |        },
        |        {
        |          "term": {
        |            "thirdCategoryId": {
        |              "value": "100210502"
        |            }
        |          }
        |        }
        |      ]
        |    }
        |  }
        |}
      """.stripMargin).values
    val frame1 = sqlContext.read.json(value1)

    import org.apache.spark.sql.functions._
    val frame = frame1.drop("fourthCategoryId").withColumn("fourthCategoryId",lit("10021050202"))

    val result = frame.toJSON.rdd.map(line => {
      val lines = JSON.parseObject(line)
      val evaluates = lines.getOrDefault("evaluates", "-1").toString
      val Base_Info = lines.getOrDefault("Base_Info", "-1").toString
      var ev = "-1"
      var str = new JSONObject
      if (evaluates.equals("-1")) {
        ev = "{\"fuyi\":\"-1\"}"
      } else {
        str = JSON.parseObject(evaluates)
      }
      if (!ev.contains("fuyi")) {
        lines.put("evaluates", str)
      } else {
        val evs = JSON.parseObject(ev)
        lines.put("evaluates", evs)
      }
      lines.put("Base_Info",Base_Info)
      lines
    })
    result.saveToEs(s"${indexs}", Map("es.mapping.id" -> "good_id"))
  }
  /**
    * 数据入es
    * @param data
    */
  def toes(data: DataFrame): Unit = {
    val value = data.toJSON.rdd.map(line => {
      val lines = JSON.parseObject(line)
      val evaluates = lines.getOrDefault("evaluates", "-1").toString
      val Base_Info = lines.getOrDefault("Base_Info", "-1").toString
      var promotion_price = lines.getOrDefault("promotion_price","-1").toString
      var original_cost = lines.getOrDefault("original_cost","-1").toString
      val priceText = lines.get("priceText").toString
      if (promotion_price.contains("?") | promotion_price.contains("？")){
        promotion_price = priceText
      }
      if (original_cost.contains("?") | original_cost.contains("？")){
        original_cost = priceText
      }
      var ev = "-1"
      var str = new JSONObject
      if (evaluates.equals("-1")) {
        ev = "{\"fuyi\":\"-1\"}"
      } else {
        str = JSON.parseObject(evaluates)
      }
      if (!ev.contains("fuyi")) {
        lines.put("evaluates", str)
      } else {
        val evs = JSON.parseObject(ev)
        lines.put("evaluates", evs)
      }
      lines.put("Base_Info",Base_Info)
      lines.put("promotion_price",promotion_price)
      lines.put("original_cost",original_cost)
      lines
    })
    //原始代码
    value.saveToEs(s"${indexs}", Map("es.mapping.id" -> "good_id"))
    //双十一代码
    //value.saveToEs(s"tmall_2019_activity/tmall_2019_11", Map("es.mapping.id" -> "good_id"))
  }

  /********************************吸尘器*************************************************************************/
  /**
    * 吸尘器拆分分类
    * @param sc
    */
  def xichenqicate(sc: SparkContext): Unit = {
    val index = s"${indexs}"
    val rdd = sc.esJsonRDD(index,
      """
        |{
        |  "query": {
        |    "bool": {
        |      "should": [
        |        {
        |          "term": {
        |            "categoryId": {
        |              "value": "124252007"
        |            }
        |          }
        |        },{
        |          "term": {
        |            "categoryId": {
        |              "value": "50008554"
        |            }
        |          }
        |        }
        |      ]
        |    }
        |  }
        |}
      """.stripMargin).values
    xichengqi(rdd.map(line => JSON.parseObject(line))).saveToEs(index, Map("es.mapping.id" -> "good_id"))
  }

  def xichengqi(rdd: RDD[JSONObject]): RDD[JSONObject] = {
    /**
      * 手持式          10014020402
      * 立式(含筒式)    筒式  10014020405
      * 卧式          10014020404
      * 杆式       10014020402
      * 肩式         10014020402
      * 直立式         10014020402
      */
    //    淘宝平台 中的除螨仪分类  124252007  。吸尘器分类 50008554
    val value = rdd.map(jsons => {
      //  ========================================================================================
      val categoryId = jsons.get("categoryId").toString


      if (categoryId.equals("50008554")) {

        val res = clear_Base(jsons, "吸尘器类型", "操作方式")
        val flag = res.get("detailCategoryName").toString
        val title = jsons.get("title").toString

        if (flag.contains("立式")) {

          if (title.contains("桶")) {
            jsons.put("fourthCategoryId", "10014020405")
            jsons.put("firstCategoryId", "10014")
            jsons.put("secondCategoryId", "1001402")
            jsons.put("thirdCategoryId", "100140204")
          } else {
            jsons.put("fourthCategoryId", "10014020402")
            jsons.put("firstCategoryId", "10014")
            jsons.put("secondCategoryId", "1001402")
            jsons.put("thirdCategoryId", "100140204")
          }

        } else if (flag.contains("手持式")) {
          jsons.put("fourthCategoryId", "10014020402")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else if (flag.contains("杆式")) {
          jsons.put("fourthCategoryId", "10014020402")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else if (flag.contains("卧式")) {
          jsons.put("fourthCategoryId", "10014020404")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else if (flag.contains("肩式")) {
          jsons.put("fourthCategoryId", "10014020402")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else if (flag.contains("直立式")) {
          jsons.put("fourthCategoryId", "10014020402")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else if (flag.contains("无线")) {
          jsons.put("fourthCategoryId", "10014020402")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        } else {
          jsons.put("fourthCategoryId", "10014020499")
          jsons.put("firstCategoryId", "10014")
          jsons.put("secondCategoryId", "1001402")
          jsons.put("thirdCategoryId", "100140204")
        }
      } else if (categoryId.equals("124252007")) {
        jsons.put("fourthCategoryId", "10014020401")
        jsons.put("firstCategoryId", "10014")
        jsons.put("secondCategoryId", "1001402")
        jsons.put("thirdCategoryId", "100140204")
      } else {
        jsons
      }
      jsons
    })
    value
  }
  /**
    * 清洗baseinfo
    * @param json
    * @return
    */
  def clear_Base(json: JSONObject, key: String*): JSONObject = {
    val keys = key.toList
    val good_id = json.get("good_id").toString
    val base = json.get("Base_Info").toString
    if (!base.equals("-1")) {
      val base_json = JSON.parseObject(base)
      var detcate = "-1"
      val title = json.get("title").toString
      val arr_key = base_json.keySet().toArray()
      for (i <- arr_key) {
        if (i.equals(keys(0))) {
          detcate = base_json.get(i).toString
        } else if (i.equals(keys(1))) {
          detcate = base_json.get(i).toString
        }
      }
      // title+"=="+detcate
      json.put("detailCategoryName", detcate)
    } else {
      json.put("detailCategoryName", "-1")
    }
    json
  }

  /********************************电脑*************************************************************************/
  /**
    * 电脑拆分分类
    * @param sc
    * @param sqlContext
    */
  def computer_cate(sc:SparkContext,sqlContext:SQLContext):Unit = {

    val computer_data = sc.esJsonRDD(s"${indexs}",
      """|{
         |  "size": 0
         |  , "query": {
         |    "bool": {
         |      "should": [
         |        {
         |          "term": {
         |            "categoryId": {
         |              "value": "1101"
         |            }
         |          }
         |        },{
         |          "term": {
         |            "categoryId": {
         |              "value": "124048001"
         |            }
         |          }
         |        }
         |      ]
         |    }
         |  }
         |}
      """.stripMargin).values
    //    sqlContext.read.json(computer_data).repartition(1).write.json(s"D:\\computer\\${platform}\\${month}")
    computer_clear(computer_data, sqlContext)
    val pingban_data = sc.esJsonRDD(s"${indexs}",
      """
        |{
        |  "size": 0
        |  , "query": {
        |    "bool": {
        |      "should": [
        |        {
        |          "term": {
        |            "thirdCategoryId": {
        |              "value": "100160101"
        |            }
        |          }
        |        }
        |      ]
        |    }
        |  }
        |}
      """.stripMargin).values
    //    sqlContext.read.json(pingban_data).repartition(1).write.json(s"D:\\computerpingban\\${platform}\\${month}")
    pingban_cate(sqlContext.read.json(pingban_data))
  }

  /**
    * 笔记本匹配
    *
    * @param data
    * @param sqlContext
    */
  def computer_clear(data: RDD[String], sqlContext: SQLContext): Unit = {

    val value3 = data.map(line => {
      val nObject = JSON.parseObject(line)
      var string = if (nObject.getOrDefault("Base_Info","""{}""").toString == "-1")
        """{}""" else {
        nObject.getOrDefault("Base_Info","""{}""").toString
      }
      val nObject1 = JSON.parseObject(string)
      nObject.put("Base_Info", nObject1)
      nObject.toString
    })

    val frame = sqlContext.read.json(value3)
    frame.registerTempTable("frame")
    val datas = sqlContext.sql(
      """
        |select
        |*,
        |IFNULL(upper(Base_Info.`CPU`),'-1') as CPUdata
        |from
        |frame
      """.stripMargin)

    val values = datas.toJSON.rdd.map(line => {
      val nObject = JSON.parseObject(line)
      val CPUdata = nObject.getOrDefault("CPUdata", "-1").toString
      val title = nObject.getOrDefault("title", "-1").toString

      /**
        * 游戏本：title中含GTX并且cpu中没有U的,title中有游戏并且cpu含H的,base_info中CPU以H结尾的为游戏本
        * 轻薄本：title中含轻薄或超薄的
        * 普通本：其他的为其他笔记本
        * 平板：平板分类+上面三个分类中品牌是微软的，并且title含pro的为平板
        */

      if ((title.toUpperCase().contains("GTX") && !CPUdata.toUpperCase().contains("U")) || (title.contains("游戏") && CPUdata.toUpperCase().contains("H"))
        || CPUdata.toUpperCase().endsWith("H")) {
        nObject.put("fourthCategoryId", "10016010101")
      }
      else if (title.contains("轻薄") || title.contains("超薄")) {
        nObject.put("fourthCategoryId", "10016010102")
      }
      else {
        nObject.put("fourthCategoryId", "10016010103")
      }
      nObject.remove("CPUdata")
      nObject.toJSONString
    })

    toes(sqlContext.read.json(values))
  }

  /**
    * 平板匹配
    *
    * @param datass
    */
  def pingban_cate(datass: DataFrame): Unit = {
    import org.apache.spark.sql.functions._
    val pingban_data = datass.where("brandName_cn = '微软/MICROSOFT' and upper(title) rlike 'PRO'")
      .drop("thirdCategoryId", "fourthCategoryId")
      .withColumn("thirdCategoryId", lit("100160102"))
      .withColumn("fourthCategoryId", lit("10016010299"))
    toes(pingban_data)
  }


  /********************************空调*************************************************************************/
  /**
    * 空调拆分分类
    * @param sc
    * @param sqlContext
    */
  def kongtiao_cate(sc:SparkContext,sqlContext:SQLContext):Unit = {

    val es_tmall = sc.esJsonRDD(s"${indexs}",
      """
        |{"size": 0,
        |  "query": {
        |    "term": {
        |      "categoryId": {
        |        "value": "350401"
        |      }
        |    }
        |  }
        |}
      """.stripMargin).values


    val unit = kongtiao(es_tmall.map(line=>JSON.parseObject(line)))
      .saveToEs(indexs, Map("es.mapping.id" -> "good_id"))
    //toes(sqlContext.read.json(unit))


  }

  /**
    * 处理空调的方法   天猫
    */
  def kongtiao(rdd:RDD[JSONObject]):RDD[JSONObject]={

    val res = rdd.map(json=> {
      val categoryId = json.get("categoryId").toString



      if(categoryId.equals("350401")) {
        val baseinfo = json.get("Base_Info").toString
        val title = json.get("title").toString
        var values = "-1"
        if (!baseinfo.equals("-1")) {
          val base = JSON.parseObject(baseinfo)
          val arr = base.keySet().toArray()
          var values = "-1"
          for (i <- arr) {
            val key = i.toString
            if (key.contains("空调类型")) {
              values = base.get(key).toString
              json.put("detailCategoryName", values)
            }
            // else if(key.contains("类别")){
            //            values = base.get(key).toString
            //            json.put("detailCategoryName",values)
            //          }
          }
        } else {
          if (title.contains("圆柱式") || title.contains("立柜") || title.contains("柜机")) {
            json.put("firstCategoryId", "10014") //立柜  10014010402
            json.put("secondCategoryId", "1001401") //立柜  10014010402
            json.put("thirdCategoryId", "100140104") //立柜  10014010402
            json.put("fourthCategoryId", "10014010402") //立柜  10014010402
          } else if (title.contains("壁挂") || title.contains("挂壁")) {
            json.put("firstCategoryId", "10014") //立柜  10014010402
            json.put("secondCategoryId", "1001401") //立柜  10014010402
            json.put("thirdCategoryId", "100140104") //立柜  10014010402
            json.put("fourthCategoryId", "10014010401") // 壁挂式 10014010401
          } else if (title.contains("移动")) {
            json.put("firstCategoryId", "10014") //立柜  10014010402
            json.put("secondCategoryId", "1001401") //立柜  10014010402
            json.put("thirdCategoryId", "100140104") //立柜  10014010402
            json.put("fourthCategoryId", "10014010403") // 移动式    10014010403
          }
        }
      }
      json
    })

    val res_2 = res.map(line=>{
      val detailCategoryName = line.getOrDefault("detailCategoryName","0").toString
      val categoryId = line.get("categoryId").toString

      if(categoryId.equals("350401")) {
        val title = line.get("title").toString
        // 资生堂惠润柔净绿野芳香600ml洗发水可悠然美肌沐浴恬静清香550ml
        if(detailCategoryName.contains("壁")){
          line.put("firstCategoryId","10014")   //立柜  10014010402
          line.put("secondCategoryId","1001401")   //立柜  10014010402
          line.put("thirdCategoryId","100140104")   //立柜  10014010402
          line.put("fourthCategoryId","10014010401")
        }else if(detailCategoryName.contains("圆柱") || detailCategoryName.contains("柜机") || detailCategoryName.contains("立柜")){
          line.put("firstCategoryId","10014")   //立柜  10014010402
          line.put("secondCategoryId","1001401")   //立柜  10014010402
          line.put("thirdCategoryId","100140104")   //立柜  10014010402
          line.put("fourthCategoryId","10014010402")
        }else if(detailCategoryName.contains("移动")){
          line.put("firstCategoryId","10014")   //立柜  10014010402
          line.put("secondCategoryId","1001401")   //立柜  10014010402
          line.put("thirdCategoryId","100140104")   //立柜  10014010402
          line.put("fourthCategoryId","10014010403")
        }else {
          if(title.contains("圆柱式") || title.contains("立柜") || title.contains("柜机")){
            line.put("firstCategoryId","10014")   //立柜  10014010402
            line.put("secondCategoryId","1001401")   //立柜  10014010402
            line.put("thirdCategoryId","100140104")   //立柜  10014010402
            line.put("fourthCategoryId","10014010402")   //立柜  10014010402
          }else if(title.contains("壁挂") ||  title.contains("挂壁")){
            line.put("firstCategoryId","10014")   //立柜  10014010402
            line.put("secondCategoryId","1001401")   //立柜  10014010402
            line.put("thirdCategoryId","100140104")   //立柜  10014010402
            line.put("fourthCategoryId","10014010401")     // 壁挂式 10014010401
          }else if(title.contains("移动")){
            line.put("firstCategoryId","10014")   //立柜  10014010402
            line.put("secondCategoryId","1001401")   //立柜  10014010402
            line.put("thirdCategoryId","100140104")   //立柜  10014010402
            line.put("fourthCategoryId","10014010403")    // 移动式    10014010403
          }else{
            line.put("firstCategoryId","10014")   //立柜  10014010402
            line.put("secondCategoryId","1001401")   //立柜  10014010402
            line.put("thirdCategoryId","100140104")   //立柜  10014010402
            line.put("fourthCategoryId","10014010499")
          }
        }
      }
      line
    })
    res_2
  }


  /********************************酸牛奶、纯牛奶*************************************************************************/
  /**
    * 酸牛奶拆分
    * 开始提取匹配
    * @param sc
    * @param spark
    */
  def suannaicate(sc:SparkContext,spark:SparkSession): Unit ={
    qita_ninai(sc.esJsonRDD(es_sql()._5,es_sql()._1).values,spark)
    println("*"*50)
    nai_chai(sc.esJsonRDD(es_sql()._5,es_sql()._2).values,spark)
    println("*"*50)
    niunai_chai(sc.esJsonRDD(es_sql()._5,es_sql()._3).values,spark)
    println("*"*50)
    suannai_chai(sc.esJsonRDD(es_sql()._5,es_sql()._4).values,spark)
    println("*"*50)
  }



  /**
    * 拆分酸奶牛奶
    * @param nai_data
    * @param spark
    */
  def nai_chai(nai_data:RDD[String],spark:SparkSession):Unit = {
    val nai_chai_complete = nai_data.map(line => {
      val nObject = JSON.parseObject(line)
      val title = nObject.getOrDefault("title", "-1").toString
      if (title.contains("酸") && title.contains("奶")) {
        //10021050202
        nObject.put("firstCategoryId", "10021")
        nObject.put("secondCategoryId", "1002105")
        nObject.put("thirdCategoryId", "100210502")
        nObject.put("fourthCategoryId", "10021050202")
      } else {
        //10021050102
        nObject.put("firstCategoryId", "10021")
        nObject.put("secondCategoryId", "1002105")
        nObject.put("thirdCategoryId", "100210501")
        nObject.put("fourthCategoryId", "10021050102")
      }
      nObject.toJSONString
    })
    toes(spark.read.json(nai_chai_complete))
  }

  /**
    * 拆分牛奶
    * @param niunai_data
    * @param spark
    */
  def niunai_chai(niunai_data:RDD[String],spark:SparkSession):Unit  ={
    val value3 = niunai_data.map(line => {
      val nObject = JSON.parseObject(line)
      var string = if (nObject.getOrDefault("Base_Info","""{}""").toString == "-1")
        """{}""" else {
        nObject.getOrDefault("Base_Info","""{}""").toString
      }
      val nObject1 = JSON.parseObject(string)
      nObject.put("Base_Info", nObject1)
      nObject.toString
    })
    val niunai_chai_complete = value3.map(line => {
      val nObject = JSON.parseObject(line)
      val title = nObject.getOrDefault("title", "-1").toString
      val Base_info = nObject.getOrDefault("Base_Info", "-1").toString
      if (title.contains("低温")) {
        nObject.put("thirdCategoryId", "100210501")
        nObject.put("fourthCategoryId", "10021050102")
      } else if (title.contains("安慕希")
        | title.contains("伊利") | (title.contains("伊利")  && (Base_info.contains("\"类别\":\"鲜奶\"") | Base_info.contains("\"类别\":\"含乳饮料\"")))) {
        if (title.contains("低温") | (title.contains("伊利")  && (Base_info.contains("\"类别\":\"鲜奶\"") | Base_info.contains("\"类别\":\"含乳饮料\"")))) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        } else if (title.contains("常温")) {
          //10021050101
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050101")
        } else {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050101")
        }
      } else if (title.contains("有机鲜奶") | title.contains("蒙牛每日鲜")) {
        if (title.contains("有机鲜奶")) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        } else if (title.contains("蒙牛每日鲜")) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        }
      } else if (Base_info.contains("\"储藏方法\":\"冷藏\"")
        | Base_info.contains("\"储藏方法\":\"低温储存\"")
        | Base_info.contains("\"储藏方法\":\"低温牛奶\"")
        | Base_info.contains("\"饮品种类\":\"低温牛奶\"")
        | Base_info.contains("\"储藏方法\":\"2°C~6°C低温冷藏\"")
        | Base_info.contains("\"储藏方法\":\"冷藏保管（0-10C°）\"")
        | Base_info.contains("\"储藏方法\":\"冰箱2-6度冷藏\"")
        | Base_info.contains("\"储藏方法\":\"低温存储\"") | title.contains("巴氏")) {
        if (Base_info.contains("\"储藏方法\":\"冷藏\"")
          | Base_info.contains("\"储藏方法\":\"低温储存\"")
          | Base_info.contains("\"储藏方法\":\"低温牛奶\"")
          | Base_info.contains("\"饮品种类\":\"低温牛奶\"")
          | Base_info.contains("\"储藏方法\":\"2°C~6°C低温冷藏\"")
          | Base_info.contains("\"储藏方法\":\"冷藏保管（0-10C°）\"")
          | Base_info.contains("\"储藏方法\":\"冰箱2-6度冷藏\"")
          | Base_info.contains("\"储藏方法\":\"低温存储\"")
        ) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        } else if (title.contains("巴氏")) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        }
        //"类别":"鲜奶"
      } else if (Base_info.contains("\"类别\":\"鲜奶\"") | Base_info.contains("\"类别\":\"酸奶\"") | Base_info.contains("\"类别\":\"含乳饮料\"")) {
        if (Base_info.contains("\"类别\":\"鲜奶\"")) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        } else if (Base_info.contains("\"类别\":\"酸奶\"")) {
          nObject.put("thirdCategoryId", "100210502")
          nObject.put("fourthCategoryId", "10021050202")
        } else if (Base_info.contains("\"类别\":\"含乳饮料\"")) {
          nObject.put("thirdCategoryId", "100210501")
          nObject.put("fourthCategoryId", "10021050102")
        }else {
          nObject.put("thirdCategoryId", "100210599")
          nObject.put("fourthCategoryId", "10021059999")
        }
      }else {
        nObject.put("thirdCategoryId", "100210501")
        nObject.put("fourthCategoryId", "10021050101")
      }
      nObject.toJSONString
    })

    toes(spark.read.json(niunai_chai_complete))
  }

  /**
    * 拆分酸奶
    * @param niunai_data
    * @param spark
    */
  def suannai_chai(niunai_data:RDD[String],spark:SparkSession):Unit  ={
    val value3 = niunai_data.map(line => {
      val nObject = JSON.parseObject(line)
      var string = if (nObject.getOrDefault("Base_Info","""{}""").toString == "-1")
        """{}""" else {
        nObject.getOrDefault("Base_Info","""{}""").toString
      }
      val nObject1 = JSON.parseObject(string)
      nObject.put("Base_Info", nObject1)
      nObject.toString
    })
    val suannai_chai_complete = value3.map(line => {
      val nObject = JSON.parseObject(line)
      val title = nObject.getOrDefault("title", "-1").toString
      val Base_info = nObject.getOrDefault("Base_Info", "-1").toString
      if (title.contains("低温")) {
        nObject.put("thirdCategoryId", "100210502")
        nObject.put("fourthCategoryId", "10021050202")
      } else if (Base_info.contains("\"储藏方法\":\"冷藏\"") //"储藏方法":"冷冻"
        | Base_info.contains("\"储藏方法\":\"低温储存\"")
        | Base_info.contains("\"储藏方法\":\"冷冻\"")
        | Base_info.contains("\"储藏方法\":\"冷藏最佳\"")
        | Base_info.contains("\"储藏方法\":\"低温存储\"")
        | Base_info.contains("\"储藏方法\":\"低温酸奶\"")
        | Base_info.contains("\"储藏方法\":\"2°C~6°C低温冷藏\"")
        | Base_info.contains("\"储藏方法\":\"冷藏保管（0-10C°）\"")
        | Base_info.contains("\"储藏方法\":\"冰箱2-6度冷藏\"")
        | Base_info.contains("\"储藏方法\":\"2-6度低温储藏\"")
        | Base_info.contains("\"储藏方法\":\"冷冻保存：密封、冷冻(0度以下)18(推荐)\"")
        | Base_info.contains("\"储藏方法\":\"0度下冷冻保存\"")
        | Base_info.contains("\"储藏方法\":\"密封，冷冻（零度以下）\"")
        | Base_info.contains("\"储藏方法\":\"密封,冷冻（零度以下）\"")
        | Base_info.contains("\"储藏方法\":\"密封 冷冻保存（零度以下）\"")
        | Base_info.contains("\"储藏方法\":\"冷冻保存 0度以下\"") //冷冻保存（0度以下）
        | Base_info.contains("\"储藏方法\":\"密封冷冻保存0度以下\"")
        | Base_info.contains("\"储藏方法\":\"冰箱冷冻保存\"")
        | Base_info.contains("\"储藏方法\":\"密封 冷冻\"") || Base_info.contains("\"储藏方法\":\"冷冻保存（0度以下）\"")
        | Base_info.contains("\"储藏方法\":\"密封冷冻\"")
        | Base_info.contains("\"储藏方法\":\"低温冷藏\"")
        | Base_info.contains("\"储藏方法\":\"零下冷冻保存\"") | title.contains("西域春")) {
        if (Base_info.contains("\"储藏方法\":\"冷藏\"") //"储藏方法":"冷冻"
          | Base_info.contains("\"储藏方法\":\"低温储存\"")
          | Base_info.contains("\"储藏方法\":\"冷冻\"")
          | Base_info.contains("\"储藏方法\":\"冷藏最佳\"")
          | Base_info.contains("\"储藏方法\":\"低温存储\"")
          | Base_info.contains("\"储藏方法\":\"低温酸奶\"")
          | Base_info.contains("\"储藏方法\":\"2°C~6°C低温冷藏\"")
          | Base_info.contains("\"储藏方法\":\"冷藏保管（0-10C°）\"")
          | Base_info.contains("\"储藏方法\":\"冰箱2-6度冷藏\"")
          | Base_info.contains("\"储藏方法\":\"2-6度低温储藏\"")
          | Base_info.contains("\"储藏方法\":\"冷冻保存：密封、冷冻(0度以下)18(推荐)\"")
          | Base_info.contains("\"储藏方法\":\"0度下冷冻保存\"")
          | Base_info.contains("\"储藏方法\":\"密封，冷冻（零度以下）\"")
          | Base_info.contains("\"储藏方法\":\"密封,冷冻（零度以下）\"")
          | Base_info.contains("\"储藏方法\":\"密封 冷冻保存（零度以下）\"")
          | Base_info.contains("\"储藏方法\":\"冷冻保存 0度以下\"") //冷冻保存（0度以下）
          | Base_info.contains("\"储藏方法\":\"密封冷冻保存0度以下\"")
          | Base_info.contains("\"储藏方法\":\"冰箱冷冻保存\"")
          | Base_info.contains("\"储藏方法\":\"密封 冷冻\"") || Base_info.contains("\"储藏方法\":\"冷冻保存（0度以下）\"")
          | Base_info.contains("\"储藏方法\":\"密封冷冻\"")
          | Base_info.contains("\"储藏方法\":\"低温冷藏\"")
          | Base_info.contains("\"储藏方法\":\"零下冷冻保存\"")
        ) {
          nObject.put("thirdCategoryId", "100210502")
          nObject.put("fourthCategoryId", "10021050202")
        } else if (title.contains("西域春")) {
          nObject.put("thirdCategoryId", "100210502")
          nObject.put("fourthCategoryId", "10021050202")
        }
      } else if (title.contains("君乐宝纯享")) {
        nObject.put("thirdCategoryId", "100210502")
        nObject.put("fourthCategoryId", "10021050202")
      }else {
        nObject.put("thirdCategoryId", "100210502")
        nObject.put("fourthCategoryId", "10021050201")
      }
      nObject.toJSONString
    })
    toes(spark.read.json(suannai_chai_complete))
  }

  /**
    * 拆分其他牛奶
    * @param qita_data
    * @param spark
    */
  def qita_ninai(qita_data:RDD[String],spark:SparkSession):Unit = {
    import org.apache.spark.sql.functions._
    val frame = spark.read.json(qita_data).drop("firstCategoryId", "secondCategoryId", "thirdCategoryId", "fourthCategoryId")
      .withColumn("firstCategoryId", lit("10021"))
      .withColumn("secondCategoryId", lit("1002105"))
      .withColumn("thirdCategoryId", lit("100210599"))
      .withColumn("fourthCategoryId", lit("10021059999"))
    toes(frame)
  }

  /**
    * es查询
    * @return
    */
  def es_sql():(String,String,String,String,String) = {
    val index = s"${indexs}"
    val query1 =
      """
        |{
        |  "size": 0
        |  , "query": {
        |    "term": {
        |      "categoryId": {
        |        "value": "50596014"
        |      }
        |    }
        |  },"aggs": {
        |    "NAME": {
        |      "terms": {
        |        "field": "fourthCategoryId",
        |        "size": 10
        |      }
        |    }
        |  }
        |}
      """.stripMargin
    val query2 =
      """
        |{
        |  "size": 0
        |  , "query": {
        |    "term": {
        |      "categoryId": {
        |        "value": "200670003"
        |      }
        |    }
        |  },"aggs": {
        |    "NAME": {
        |      "terms": {
        |        "field": "fourthCategoryId",
        |        "size": 10
        |      }
        |    }
        |  }
        |}
      """.stripMargin
    val query3 =
      """
        |{
        |  "size": 0,
        |  "query": {
        |    "bool": {
        |      "must": [
        |        {
        |          "match_phrase": {
        |            "thirdCategoryId": "100210501"
        |          }
        |        }
        |      ]
        |    }
        |  },"aggs": {
        |    "NAME": {
        |      "terms": {
        |        "field": "fourthCategoryId",
        |        "size": 10
        |      }
        |    }
        |  }
        |}
      """.stripMargin
    val query4 =
      """
        |{
        |  "size": 0,
        |  "query": {
        |    "bool": {
        |      "must": [
        |        {
        |          "match_phrase": {
        |            "thirdCategoryId": "100210502"
        |          }
        |        }
        |      ]
        |    }
        |  },"aggs": {
        |    "NAME": {
        |      "terms": {
        |        "field": "fourthCategoryId",
        |        "size": 10
        |      }
        |    }
        |  }
        |}
      """.stripMargin
    (query1,query2,query3,query4,index)
  }


  /***************************************乳液面霜*********************************************************/
  import a_aa_amainpackage.a_o2odata_deal.utils.RuYeMianSuang_clear._
  def ruyemianshuang(sc:SparkContext,spark:SparkSession):Unit  ={
      ruyemianshuang_cateclear(sc,spark)
      val ruyemianshuang_res = spark.read.json(s"${ruyemianshuangpath}")
      toes(ruyemianshuang_res)
  }

  /****************************************其他分类拆分******************************************************************/
  def other_cate(sc:SparkContext,sqlContext:SQLContext):Unit = {
    val values: RDD[String] = sc.esJsonRDD(s"${indexs}").values
    val sourcedata = sqlContext.read.json(values).where("categoryId  = '50010535' or  categoryId  = '123256002' or categoryId  = '50008059' or categoryId  = '123210006'" +
      "or categoryId  = '121422025' or categoryId  = '50008056' or categoryId  = '50016092' or categoryId  = '124302002'" +
      "or categoryId  = '124312003' or categoryId  = '124320001' or categoryId  = '124456021' or categoryId  = '124466010'" +
      "or categoryId  = '124468007' or categoryId  = '124478011' or categoryId  = '124492008' or categoryId  = '124512008'" +
      "or categoryId  = '126474004' or categoryId  = '50008055' or categoryId  = '124292002' or categoryId  = '124296001'" +
      "or categoryId  = '124300001' or categoryId  = '124302001' or categoryId  = '124304001' or categoryId  = '124306001'" +
      "or categoryId  = '124308001' or categoryId  = '124310001' or categoryId  = '124312001' or categoryId  = '124604001'" +
      "or categoryId  = '124606001' or categoryId  = '124606002' or categoryId  = '50008062' or categoryId  = '50010513'" +
      "or categoryId  = '50012598' or categoryId  = '50010511' or categoryId  = '124298001' or categoryId  = '1101' or  categoryId  = '124048001'  "
    )
    val value = snacks_cate_modify(sourcedata.toJSON.rdd)
    toes(sqlContext.read.json(value))
  }
  def snacks_cate_modify(data: RDD[String]): RDD[String] = {
    val da = data.map { line =>
      val nObject = JSON.parseObject(line)
      val categoryId = nObject.get("categoryId").toString
      val title = nObject.get("title").toString
      val Base_Info = nObject.get("Base_Info").toString

      if (categoryId.equals("50010535") || categoryId.equals("123256002") ||
        categoryId.equals("50008059") || categoryId.equals("123210006")
      ) {
        if (title.contains("坚果大礼包") || title.contains("坚果组合") || title.contains("坚果炒货") || title.contains("坚果礼盒") || title.contains("每日坚果")
          || title.contains("坚果零食大礼包") || title.contains("坚果混合") || title.contains("混合坚果") || title.contains("坚果干果混合")) {
          nObject.put("thirdCategoryId", "100210102")
        } else if ((title.contains("混装") || title.contains("组合整箱"))  ) {
          nObject.put("thirdCategoryId", "100210199")
        } else if ( title.contains("薯片") || title.contains("爆米花") || title.contains("山药薄片") || title.contains("虾片") || title.contains("虾条")
          || title.contains("雪米饼") || title.contains("锅巴") || title.contains("烤香馍片") || title.contains("拉面丸子")
          || title.contains("仙贝")) {
          nObject.put("thirdCategoryId", "100210108")
        } else if (title.contains("威化饼干") || title.contains("手指饼干") || title.contains("奶酪饼干") || title.contains("糙米卷") || title.contains("饼干糕点")
          || title.contains("香葱饼干") || title.contains("字母饼干") || title.contains("饼干碎屑") || title.contains("薄脆饼干") || title.contains("夹心饼干")) {
          nObject.put("thirdCategoryId", "100210104")
        } else if (title.contains("牛奶夹心巧克力") || title.contains("巧克力豆") || title.contains("星球杯") || title.contains("奇趣蛋")
          || title.contains("巧克力礼盒")) {
          nObject.put("thirdCategoryId", "100210101")
        } else if (title.contains("棒棒糖") || title.contains("QQ糖") || title.contains("水果") && title.contains("软糖") ||
          title.contains("混合装糖果") || title.contains("薄荷味糖")
          || title.contains("口香糖") || title.contains("水果糖") || title.contains("紫皮糖") || title.contains("玉米软糖") ||
          title.contains("什锦糖") || title.contains("软心球")
          || title.contains("什锦喜庆糖") || title.contains("牛轧糖") || title.contains("酥心糖")) {
          nObject.put("thirdCategoryId", "100210103")
        }
        else{
          nObject.put("thirdCategoryId", "100210199")
        }
        nObject.put("secondCategoryId", "1002101")
        nObject.put("firstCategoryId", "10021")


        //title.contains("轻薄") || title.contains("超薄"
      } else if (categoryId.equals("1101") || categoryId.equals("124048001")) {
        if (title.toUpperCase().contains("GTX")) {
          nObject.put("fourthCategoryId", "10016010101")
        }
        else if (title.contains("轻薄") || title.contains("超薄")) {
          nObject.put("fourthCategoryId", "10016010102")
        } else {
          nObject.put("fourthCategoryId", "10016010103")
        }
        nObject.put("thirdCategoryId", "100160101")
        nObject.put("secondCategoryId", "1001601")
        nObject.put("firstCategoryId", "10016")
      }
      else if (categoryId.equals("121422025") || categoryId.equals("50008056") ||
        categoryId.equals("50016092")) {
        if (title.contains("原料") || title.contains("泡泡糖") || title.contains("口香糖")) {
          nObject.put("thirdCategoryId", "100210103")
        } else if (title.contains("牛奶夹心巧克力") || title.contains("巧克力豆") || title.contains("星球杯") || title.contains("奇趣蛋") || title.contains("巧克力礼盒")) {
          nObject.put("thirdCategoryId", "100210101")
        } else if (title.contains("薯片") || title.contains("爆米花") || title.contains("虾片") || title.contains("虾条")
          || title.contains("雪米饼") || title.contains("锅巴") || title.contains("烤香馍片") || title.contains("拉面丸子")
          || title.contains("仙贝")) {
          nObject.put("thirdCategoryId", "100210108")
        } else if (title.contains("手撕面包") || title.contains("小口袋面包") || title.contains("京八件") || title.contains("华夫饼") || title.contains("月饼")
          || title.contains("糕点礼盒") || title.contains("曲奇饼干") || title.contains("蒸蛋糕") || title.contains("拿破仑蛋糕") || title.contains("凤梨酥")
          || title.contains("沙琪玛") || title.contains("高纤粗粮饼") || title.contains("蛋卷")
          || title.contains("夹心饼干") || title.contains("饼干棒") || title.contains("核桃饼") || title.contains("巧克力饼干")) {
          nObject.put("thirdCategoryId", "100210104")
        } else {
          nObject.put("thirdCategoryId", "100210103")
        }
        nObject.put("secondCategoryId", "1002101")
        nObject.put("firstCategoryId", "10021")



      } else if (categoryId.equals("124302002") || categoryId.equals("124312003") ||
        categoryId.equals("124320001") || categoryId.equals("124456021") ||
        categoryId.equals("124466010") || categoryId.equals("124468007") ||
        categoryId.equals("124478011") || categoryId.equals("124492008") ||
        categoryId.equals("124512008") || categoryId.equals("126474004") ||
        categoryId.equals("50008055")) {
        if (title.contains("巧克力礼盒") || title.contains("星球杯") || title.contains("奇趣蛋") || title.contains("坚果大礼包") || title.contains("每日坚果")
          || title.contains("混合坚果") || title.contains("坚果混合") || title.contains("薯片") || title.contains("爆米花") || title.contains("虾片")
          || title.contains("虾条") || title.contains("雪米饼") || title.contains("锅巴") || title.contains("烤香馍片") || title.contains("拉面丸子")
          || title.contains("仙贝")
          || title.contains("手撕面包") || title.contains("小口袋面包") || title.contains("京八件") || title.contains("华夫饼") || title.contains("月饼")
          || title.contains("糕点礼盒") || title.contains("曲奇饼干") || title.contains("蒸蛋糕") || title.contains("拿破仑蛋糕") || title.contains("凤梨酥")
          || title.contains("沙琪玛") || title.contains("高纤粗粮饼") || title.contains("蛋卷")
          || title.contains("夹心饼干") || title.contains("核桃饼") || title.contains("巧克力饼干") || title.contains("棒棒糖") || title.contains("QQ糖")
          || title.contains("水果") && title.contains("软糖") || title.contains("混合装糖果") || title.contains("薄荷味糖")
          || title.contains("口香糖") || title.contains("水果糖") || title.contains("紫皮糖") || title.contains("玉米软糖")
          || title.contains("什锦糖") || title.contains("软心球")
          || title.contains("什锦喜庆糖") || title.contains("牛轧糖") || title.contains("酥心糖")) (

          if (title.contains("坚果大礼包") || title.contains("每日坚果") || title.contains("混合坚果") || title.contains("坚果混合")) {
            nObject.put("thirdCategoryId", "100210102")

          } else if (title.contains("薯片") || title.contains("爆米花") || title.contains("虾片") || title.contains("虾条")
            || title.contains("雪米饼") || title.contains("锅巴") || title.contains("烤香馍片") || title.contains("拉面丸子") || title.contains("仙贝")) {
            nObject.put("thirdCategoryId", "100210108")


          } else if (title.contains("手撕面包") || title.contains("小口袋面包") || title.contains("京八件") || title.contains("华夫饼") || title.contains("月饼")
            || title.contains("糕点礼盒") || title.contains("曲奇饼干") || title.contains("蒸蛋糕") || title.contains("拿破仑蛋糕") || title.contains("凤梨酥")
            || title.contains("沙琪玛") || title.contains("高纤粗粮饼") || title.contains("蛋卷")
            || title.contains("夹心饼干") || title.contains("核桃饼") || title.contains("巧克力饼干")) {
            nObject.put("thirdCategoryId", "100210104")
          } else if (title.contains("棒棒糖") || title.contains("QQ糖") || title.contains("水果") && title.contains("软糖") || title.contains("混合装糖果")
            || title.contains("薄荷味糖")
            || title.contains("口香糖") || title.contains("水果糖") || title.contains("紫皮糖") || title.contains("玉米软糖") || title.contains("什锦糖")
            || title.contains("软心球")
            || title.contains("什锦喜庆糖") || title.contains("牛轧糖") || title.contains("酥心糖")) {
            nObject.put("thirdCategoryId", "100210103")
          } else {
            nObject.put("thirdCategoryId", "100210101")
          }) else {
          nObject.put("thirdCategoryId", "100210101")
        }
        nObject.put("secondCategoryId", "1002101")
        nObject.put("firstCategoryId", "10021")




      } else if (categoryId.equals("124292002") || categoryId.equals("124296001") ||
        categoryId.equals("124296001") || categoryId.equals("124300001") ||
        categoryId.equals("124302001") || categoryId.equals("124304001") ||
        categoryId.equals("124306001") || categoryId.equals("124308001") ||
        categoryId.equals("124310001") || categoryId.equals("124312001") ||
        categoryId.equals("124604001") || categoryId.equals("124606001") ||
        categoryId.equals("124606002") || categoryId.equals("50008062") ||
        categoryId.equals("50010513") || categoryId.equals("50012598") ||
        categoryId.equals("50010511") || categoryId.equals("124298001")
      ) {
        if (title.contains("大礼包") || title.contains("混装")) {
          nObject.put("thirdCategoryId", "100210104")
          //徐福记米格玛夹心米果卷饼干495g散称儿童过年零食 年货点心批发
        } else if (title.contains("薯片") || title.contains("爆米花") || title.contains("山药薄片") || title.contains("虾片") || title.contains("虾条")
          || title.contains("雪米饼") || title.contains("锅巴") || title.contains("烤香馍片") || title.contains("拉面丸子")
          || title.contains("仙贝")) {
          nObject.put("thirdCategoryId", "100210108")
        } else if (title.contains("棒棒糖") || title.contains("QQ糖") || title.contains("水果") && title.contains("软糖") || title.contains("混合装糖果") || title.contains("薄荷味糖")
          || title.contains("口香糖") || title.contains("水果糖") || title.contains("紫皮糖") || title.contains("玉米软糖") || title.contains("什锦糖") || title.contains("软心球")
          || title.contains("什锦喜庆糖") || title.contains("牛轧糖") || title.contains("酥心糖")) {
          nObject.put("thirdCategoryId", "100210103")
        } else {
          nObject.put("thirdCategoryId", "100210104")
        }
        nObject.put("secondCategoryId", "1002101")
        nObject.put("firstCategoryId", "10021")
      }
      nObject.toString
    }
    da
  }

  /****************************************血糖仪拆分******************************************************************/
  def xuetangyi(sc:SparkContext,sqlContext:SQLContext):Unit = {
    val value2: RDD[String] = sc.esJsonRDD(s"${indexs}",
      """
        |{
        |  "query": {
        |    "term": {
        |      "categoryId": {
        |        "value": "50023744"
        |      }
        |    }
        |  }
        |}
      """.stripMargin).values
    val value3 = value2.map(line => {
      val nObject = JSON.parseObject(line)
      var string = if (nObject.getOrDefault("Base_Info","""{}""").toString == "-1")
        """{}""" else {
        nObject.getOrDefault("Base_Info","""{}""").toString
      }
      val nObject1 = JSON.parseObject(string)
      nObject.put("Base_Info", nObject1)
      nObject.toString
    })
    var value4 = value3.map(x => {
      val nObject = JSON.parseObject(x)
      val title = nObject.get("title").toString

      var Base_Info = nObject.get("Base_Info").toString

      val nobject1 = JSON.parseObject(Base_Info)
      val yongpin = nobject1.getOrDefault("血糖用品","-1").toString
      var thirdCategoryId = nObject.get("thirdCategoryId")


      if (title.contains("套装")
        || title.contains("+仪器】")
        || title.contains("仪器+")
        || title.contains("血糖仪家用+")
        || title.contains("免调码全自动语音")
        || title.contains("智能语音播报")
        || title.contains("三诺安稳+")
        || title.contains("智能免条码家用血糖测试仪")
        || title.contains("血糖测试仪家用+")
        || title.contains("测血糖精准智能仪器")
        || ((!(yongpin.contains("血糖试纸") && yongpin.contains("采血针/采血器") && yongpin.contains("血糖用品配件"))
        && yongpin.contains("胰岛素笔+针头") && yongpin.contains("胰岛素冷藏盒") && yongpin.contains("胰岛素笔")
        && yongpin.contains("血糖试纸+针头") && yongpin.contains("血糖仪器 ")
        && yongpin.contains("胰岛素注射用品")) && yongpin.contains("血糖仪+试纸+针头组合套装"))
        || (yongpin.contains("血糖仪+试纸+针头组合套装 血糖仪器") && !yongpin.contains("血糖试纸"))
        || title.contains("仪器")
        || title.contains("血糖仪+")
        || title.contains("血糖测试仪罗康全活力配")
        || title.contains("德国进口测试仪")
        || title.contains("血糖仪配")
        || title.contains("血糖仪送")
        || title.contains("全自动血糖测试医仪")
        || title.contains("血糖仪") && title.contains("送")
        || title.contains("智能调码医用血糖仪")
        || title.contains("血糖仪 配")
        || title.contains("怡成5秒血糖仪")
        || title.contains("血糖仪稳豪倍易型+")
        || title.contains("安稳+")
        || title.contains("血糖仪单机")
        || title.contains("拜安康+")
        || title.contains("单机")
      ) {
        thirdCategoryId = "100250802"
      } else if (
        title.contains("血糖试纸") && !title.contains("仪")
          || (yongpin.contains("采血针/采血器") && !yongpin.contains("血糖仪"))
          || (yongpin.contains("血糖试纸+针头") && !yongpin.contains("血糖仪"))
          || (yongpin.contains("血糖试纸+针头") && !yongpin.contains("血糖仪"))
          || (yongpin.contains("胰岛素注射用品"))
          || (title.contains("胰岛素"))
          || ((title.contains("血糖仪")) && title.contains("适用"))
          || (title.contains("血糖仪试纸"))
          || (title.contains("血糖仪测试片"))
          || (title.contains("巧型利舒坦血糖试纸"))
          || (!title.contains("血糖仪"))
          || (title.contains("刺络排瘀针"))
          || (title.contains("测试仪试纸升级"))
          || (title.contains("收纳包"))
          || (title.contains("胰岛素"))
          || (title.contains("血糖测试条"))
          || (title.contains("血糖仪针头"))
          || (title.contains("采血笔"))
          || (title.contains("越巧试纸50片"))
          || (title.contains("血糖仪血糖试纸")) && !title.contains("血糖仪")
          || (title.contains("家用电子血糖仪试片"))
          || (title.contains("罗氏血糖试纸"))
          || (title.contains("测试测量仪试条"))
          || (title.contains("血糖仪采血笔"))
          || (title.contains("独立装血糖试纸"))
          || (title.contains("糖仪家用5d-1型怡成血糖试"))
          || (title.contains("血糖仪5D-1血糖试纸"))
          || (title.contains("通用")) && title.contains("血糖仪")
          || (title.contains("A-6智能语音测血糖仪独立装试纸50片"))
          || (title.contains("怡成血糖测试仪5d-1血糖试纸"))
          || (title.contains("艾科ezⅢ血糖测试条家用血糖仪"))
      ) {
        thirdCategoryId = "100250899"
      }
      else {
        thirdCategoryId = "100250802"
      }

      nObject.put("thirdCategoryId", thirdCategoryId)

      nObject.toJSONString
    }
    )
    toes(sqlContext.read.json(value4))
  }




}
